package FIT::Document::Transformer::AddRows;

use FIT::Class;
use FIT::Document::Parser::Twig;

extends 'FIT::Document::Transformer::Base';

has rows => (is => 'ro', isa => 'ArrayRef', required => 1);

sub _transform {
    my $table = shift;
    my $body  = $table->get_body;
    my $first_cell = $body->first_cell;
    my $max_colspan;
    if ($first_cell) { $max_colspan = $first_cell->att('colspan') || 0 }
    my @rows;
    for my $row_data (@{ $self->rows }) {
        my $row = XML::Twig::Elt->new('tr');
        push @rows, $row;
        $row->paste(last_child => $body);
        if ($first_cell) { $max_colspan = @$row_data if @$row_data > $max_colspan }
        for my $cell_data (@$row_data) {
            my $cell = XML::Twig::Elt->new('td');
            $cell->paste(last_child => $row);
            $cell->set_text( $self->escape( $cell_data ) );
        }
    }
    if ($first_cell) { $first_cell->set_att(colspan => $max_colspan) }
    return @rows;
}

